package com.drinks.yiniang.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.alibaba.fastjson.JSON;
import com.hansen.library.pickerview.bean.city.CityJson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * TODO<读取Json文件的工具类>
 *
 * @author: 小嵩
 * @date: 2017/3/16 16:22

 */

public class GetJsonDataUtil {

    public static String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static ArrayList<CityJson> parseData(String result) {//Gson 解析
        ArrayList<CityJson> detail = new ArrayList<>();

        try {
            detail = (ArrayList<CityJson>) JSON.parseArray(result, CityJson.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return detail;
    }

    public static CityJson parseAllCountryData() {//Gson 解析
        String result = "{\"region_name\":\"全国\",\"items\":[{\"region_name\":\"全国\",\"items\":[{\"region_name\":\"全国\"}]}]}";

        CityJson detail = null;

        try {
            detail = JSON.parseObject(result, CityJson.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }
}

